<template>
  <!-- 级联组件 -->
  <el-cascader
    :value="value"
    separator="-"
    size="mini"
    :props="props"
    :options="depts"
    @change="handleChange"
  />
</template>

<script>
import { getDepartment } from '@/api/department'
import { translateListToTreeData } from '@/utils'

export default {
  props: {
    value: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      depts: [],
      props: {
        value: 'id', // 存储的字段 (发送给后端)
        label: 'name' // 展示的字段
      }
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    // 选择的部门发生改变
    handleChange(list) {
      if (list.length) {
        this.$emit('input', list[list.length - 1])
      } else {
        this.$emit('input', null)
      }
    },
    // 获取部门列表
    async getDepartment() {
      const res = await getDepartment()
      this.depts = translateListToTreeData(res, 0)
      console.log(this.depts)
    }
  }
}
</script>

<style scoped lang='scss'>

</style>
